//
//  DCChatExtraView.m
//  xmppBase
//
//  Created by striby on 16/1/8.
//  Copyright © 2016年 com.dc. All rights reserved.
//

#import "DCChatExtraView.h"
@interface DCChatExtraView ()
{
    
}
@end


@implementation DCChatExtraView
+(DCChatExtraView *)shareInstanceInView:(UIView *)superView
{
  
    static DCChatExtraView * instance = nil;
    
    static dispatch_once_t once ;
    
    dispatch_once(&once, ^{
        instance = [[self alloc]init];
        [instance initBaseUIInSuperView:superView];
    });
    
    if (instance.pageControl) {
        instance.pageControl.currentPage=0;
    }
    if (instance.contentScrollView) {
        [instance. contentScrollView setContentOffset:CGPointMake(0, 0)];
    }
    
    [instance setFrame:CGRectMake(0, superView.bounds.size.height, DCDeviceWidth, chatExtraViewHeight)];
    
    return instance;
}

-(void)initBaseUIInSuperView:(UIView*)superView
{
    self.backgroundColor = [UIColor whiteColor];
    
    [self setFrame:CGRectMake(0, superView.bounds.size.height, DCDeviceWidth, chatExtraViewHeight)];
    
    _contentScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, DCDeviceWidth, chatExtraViewHeight-pageControlHeight)];
    _contentScrollView.showsHorizontalScrollIndicator=NO;
    _contentScrollView.showsVerticalScrollIndicator=NO;
    _contentScrollView.pagingEnabled=YES;
    _contentScrollView.delegate=self;
    _contentScrollView.bounces=NO;
    [self addSubview:_contentScrollView];

    UIImageView * line = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, .5)];
    [line setImage:[UIImage imageNamed:@"case_cell_Line"]];
    [self addSubview:line];
    
}

-(void)setItems:(NSArray *)items
{
    if (_pageControl) {
        [_pageControl removeFromSuperview];
        _pageControl = nil;
    }
    
    for ( UIView * view in _contentScrollView.subviews) {
        [view removeFromSuperview];
    }
    
    float itemWidth = DCDeviceWidth/4*extraIconRate;
    
    float itemMargin = (DCDeviceWidth-(itemWidth*4))/5;
    
    
    
    int currentPage = 0;
    
    int currentLine = 0;
    
    int btnCountInLine = 4;
    
    
    
    for (int i=0; i<items.count; i++) {
        
        if (i!=0&&i%btnCountInLine==0) {
            currentLine++;
            if (currentLine==2) {
                currentLine=0;
            }
        }
        
        if (i!=0&&i%(btnCountInLine*2)==0) {
            currentPage++;
        }
        
        UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(itemMargin*(i%btnCountInLine+1)+currentPage*DCDeviceWidth+(i%btnCountInLine*itemWidth), currentLine*_contentScrollView.bounds.size.height/2, itemWidth, _contentScrollView.bounds.size.height/2)];
     
        btn.tag = i;
        
        [btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
        
        [btn setImage:[UIImage imageNamed:items[i][extraIcon]] forState:UIControlStateNormal];
        
         [btn.imageView setContentMode:UIViewContentModeScaleAspectFit];
        
        [btn setContentEdgeInsets:UIEdgeInsetsMake(btn.contentEdgeInsets.top-4, btn.contentEdgeInsets.left, btn.contentEdgeInsets.bottom, btn.contentEdgeInsets.right)];
        
        UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, btn.bounds.size.height-[UIFont systemFontOfSize:labelFontSize ].lineHeight, btn.bounds.size.width, [UIFont systemFontOfSize:labelFontSize ].lineHeight)];
        label.textAlignment = NSTextAlignmentCenter;
        label.backgroundColor = [UIColor clearColor];
        label.textColor = [UIColor lightGrayColor];
        label.font = [UIFont systemFontOfSize:labelFontSize];
        label.text = items[i][extraName];
        [btn addSubview:label];
        [_contentScrollView addSubview:btn];
        
    }
    
    [_contentScrollView setContentSize:CGSizeMake(DCDeviceWidth*(items.count/8+1), _contentScrollView.bounds.size.height)];
    
    if (items.count/8+1!=1) {
        _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, chatExtraViewHeight-pageControlHeight, DCDeviceWidth, pageControlHeight)];
        _pageControl.numberOfPages =items.count/8+1;
        _pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
        _pageControl.currentPageIndicatorTintColor = [UIColor darkGrayColor];
        [self addSubview:_pageControl];
    }
    
}

-(void)btnPressed:(UIButton *)sender
{
    if (self.delegate) {
        if ([self.delegate respondsToSelector:@selector(chatExtraView:selectedForIndex:)]) {
            [self.delegate chatExtraView:self selectedForIndex:sender.tag];
        }
    }
}

-(void)showExtraViewWithDuration:(float)duration isUsingAnination:(BOOL)useAnimation animations:(void (^ __nullable)(void))animations completion:(void (^ __nullable)(BOOL finished))completion
{
    if (useAnimation) {
        [UIView animateWithDuration:duration animations:^{
            [self setFrame:CGRectMake(0, self.superview.bounds.size.height-chatExtraViewHeight, self.bounds.size.width, self.bounds.size.height)];
            if (animations) {
                animations();
            }
            
        }completion:^(BOOL finished) {
            if (completion) {
                completion(finished);
            }
            
        }];
    }
    else
    {
        [self setFrame:CGRectMake(0, self.superview.bounds.size.height-chatExtraViewHeight, self.bounds.size.width, self.bounds.size.height)];
        if (animations) {
            animations();
        }
        if (completion) {
            completion(YES);
        }

    }
    
}

-(void)hideExtraViewWithDuration:(float)duration isUsingAnination:(BOOL)useAnimation animations:(void (^ __nullable)(void))animations completion:(void (^ __nullable)(BOOL finished))completion
{
    if (useAnimation) {
        [UIView animateWithDuration:duration animations:^{
            
            [self setFrame:CGRectMake(0, self.superview.bounds.size.height, self.bounds.size.width, self.bounds.size.height)];
            if (animations) {
                animations();
            }
        }completion:^(BOOL finished) {
            if (completion) {
                completion(finished);
            }
        }];

    }
    else
    {
         [self setFrame:CGRectMake(0, self.superview.bounds.size.height, self.bounds.size.width, self.bounds.size.height)];
        if (animations) {
            animations();
        }
        if (completion) {
            completion(YES);
        }
    }
    
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView==_contentScrollView) {
        
        int pageIndex = scrollView.contentOffset.x/DCDeviceWidth;
        
        _pageControl.currentPage = pageIndex;
        
    }
}

@end
